package com.bkhech.home.practice.spring.el;

import lombok.Data;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.List;

/**
 *  Parser Configuration Demo
 * @author guowm
 * @date 2023/1/16
 */
public class ParserConfigurationDemo {

    @Data
    static class Demo {
        public List<String> list;
    }

    @Data
    static class Demo1 {
        public String payload;
    }

    public static void main(String[] args) {
        // Turn on:
        // - auto null reference initialization
        // - auto collection growing
        SpelParserConfiguration config = new SpelParserConfiguration(true, true);
        ExpressionParser parser = new SpelExpressionParser(config);
        final Expression expression = parser.parseExpression("list[3]");

        Demo demo = new Demo();
        final Object value = expression.getValue(demo);
        System.out.println(value);


        SpelParserConfiguration config1 = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, ParserConfigurationDemo.class.getClassLoader());
        ExpressionParser parser1 = new SpelExpressionParser(config1);
        final Expression expression1 = parser1.parseExpression("payload");

        Demo1 demo1 = new Demo1();
        final Object value1 = expression1.getValue(demo1);
        System.out.println(value1);

    }

}
